var _pagination = null;
var cache = null;
var _columnId;
$(function() {
});

/**
 * 查询
 */
function search() {
	var content = $("#searchName1").val();
	console.log(content);
	_pagination = new Pagination({
		url : '../column/selecter',
		data : {
			content : content
		},
		success : printListToPage,
		pagerHandler : 'pager'
	});
}

/**
 * 显示
 */
function printListToPage(rst) {
	var response = new qmsk.Response(rst);
	cache = response;
	qmsk.dataTable({
		tbodyId : 'columnList',
		heads : ["columnId", "columnName", "columnId"],
		response : response,
		handler : [[2, updateAndDelButton]],
		sup : ['sup', '共 [index] 条']
	});

}

function updateAndDelButton(columnId, row) {
	return "<input class='del' type='button' value='删除' onClick='del(" + columnId + ")'/>" + "<input type='button' class='common_btn' value='修改' onClick='toUpdate(" + row + ")'/>";
}

/**
 * 打开添加弹出层
 */
function showAddPanel() {

	$("#saveButton").unbind('click');
	$("#saveButton").bind('click', add);

	$("#window_change input[type='text']").val('');

	$("#window_change,#yinying1").show();
	addMargin($("#window_change"));
}

/**
 * 隐藏添加弹出层
 */
function hideAddPanel() {
	$("#window_change,#yinying1").hide();
}

function add() {

	var columnName = $("#addName").val();

	qmsk.post("../column/insert", {
		columnName : columnName
	}, function() {
		artSuccess("添加成功！");
		hideAddPanel();
		if (_pagination) {
			_pagination.reload();
		}
	});
}

function del(columnId) {
	artConfirm('确定要删除吗？', function() {
		qmsk.post('../column/del/' + columnId, function() {
			artSuccess("删除成功！");
			_pagination.reload();
		});
	});
}

function toUpdate(row) {

	var response = cache;

	var columnId = response.getValue("columnId", row);
	var columnName = response.getValue("columnName", row);

	_columnId = columnId;
	$("#addName").val(columnName);

	$("#window_change,#yinying1").show();
	addMargin($("#window_change"));

	$("#saveButton").unbind('click');
	$("#saveButton").bind('click', update);

}

function update() {
	var columnName = $("#addName").val();
	qmsk.post("../column/update/" + _columnId, {
		columnName : columnName
	}, function(respone) {
		artSuccess("更新成功！");
		$("#window_change,#yinying1").hide();
		_pagination.reload();
	});
}

